<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition template="/HomeTemplate.xhtml"
                xmlns="http://www.w3.org/1999/xhtml"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:p="http://primefaces.org/ui"
                >
    <ui:define name="body">
        <div class="main-container">

            <h:form id="form">  

                <p:growl id="growl" showDetail="true" life="3000" />  

                <p:dialog id="dialog" header="Login" widgetVar="dlg" resizable="false" visible="true" closable="false">  

                    <h:panelGrid columns="2" cellpadding="5">  
                        <h:outputLabel for="username" value="Username:" />  
                        <p:inputText value="#{loginBean.userName}"   
                                     id="username" required="true" label="username" />  

                        <h:outputLabel for="password" value="Password:" />  
                        <p:password value="#{loginBean.password}"   
                                    id="password" required="true" label="password" />  

                        <f:facet name="footer">  
                            <p:commandButton id="loginButton" value="Login" update="growl, panelProf"    
                                             actionListener="#{loginBean.login()}" 
                                             oncomplete="handleLoginRequest(xhr, status, args)"/>  
                        </f:facet>  
                    </h:panelGrid>  

                </p:dialog>  


                <p:panel id="panelProf" widgetVar="panelProfiles" visible="false" style="border-width: 0px;">
                    <h:panelGrid columns="1"  style="border-bottom-width: 0px; border-spacing: 0px; ">
                        <p:selectOneMenu id="selectProf" value="#{loginBean.selectedProfile}" style="width: 150px;">
                            <f:selectItem itemLabel="Select Profile" itemValue=""  />
                            <f:selectItems value="#{loginBean.userProfiles}" var="prof"
                                           itemLabel="#{prof.name}" itemValue="#{prof.idProfile}"/>
                        </p:selectOneMenu>
                        <p:commandButton id="profileButton" value="Select Profile"     
                                         action="#{loginBean.selectProfile}" ajax="false"/> 
                    </h:panelGrid>

                </p:panel>


            </h:form>  

            <script type="text/javascript">
                function handleLoginRequest(xhr, status, args) {
                    if (args.validationFailed || !args.loggedIn) {
                        dlg.jq.effect("shake", {times: 5}, 100);
                    } else if (args.isPOS)
                    {
                        dlg.hide();
                        window.location = '#{request.contextPath}/pos/HomePage.xhtml';
                    }
                    else {
                        dlg.hide();
                        panelProfiles.show();
                    }
                }
            </script>    
        </div>
    </ui:define>
</ui:composition>

